import com.powerfl.frm.taf.*;

final public function arrayToString( $array:Array ):String{
	return '[' + $array.join( ',' ) +']';
}
static public function arrayStripData( $target:Array ):Array{
	var i:uint, j:uint, k:uint, l:uint, temp:Array;
	for( i = 0, j = $target.length ; i < j ; ++i ){
		if( $target[i] is Array ){
			temp = arrayStripData( $target[i] );
			$target.splice( i, 1 );
			for( l = 0, k = temp.length ; l < k ; ++l, ++i, ++j ){
				$target.splice( i, 0, temp[l] );
			}
		}
	}
	return $target;
}
static public function arrayStrip( ...$target ):Array{
	var i:uint, j:uint, temp:Array;
	if( $target.length ){
		$target = $target.concat();
		temp = [0, 0];
		for( i = 0, j = $target.length ; i < j ; ++i ){
			if( $target[i] is Array ){
				temp[0] = i;
				$target.splice.apply( null, temp.concat( arrayStripData( $target.splice( i, 1 ) ) ) );
			}
		}
	}
	return $target;
}
static public function arrayClone( $data:Array, $apply:Function = null ):Array{
	var result:Array, i:int, j:int;
	result = [];
	for( i = 0, j = $data.length ; i < j ; ++i ){
		if( $data[i] is Array ){
			result[i] = arrayClone( $data[i], $apply );
		}else if( $data[i].toString() === '[object Object]' ){
			result[i] = objectClone( $data[i] );
		}else{
			if( $apply is Function){
				result[i] = $apply( i, $data[i], $data );	
			}else{
				result[i] = $data[i];
			}
		}
	}
	return result;
}
final public function arrayFromObject( $data:Object, $addField:Array = null, ...$ignoreKeys ):Array{
	var result:Array, key:*;
	result = [];
	for( key in $data ){
		if( $ignoreKeys.indexOf( key ) === -1 ){
			result[result.length] = [key, $data[key]];
			if( $addField ){
				result[result.length - 1] = result[result.length - 1].concat( $addField );
			}
		}
	}
	return result;
}
final public function arrayFromKey( $data:Object, ...$ignoreKeys ):Array{
	var result:Array, key:*;
	result = [];
	for( key in $data ){
		if( $ignoreKeys.indexOf( key ) === -1 ){
			result[result.length] = key;
		}
	}
	return result;
}
final public function arrayFromData( $data:Object, ...$ignoreKeys ):Array{
	var result:Array, key:*;
	result = [];
	for( key in $data ){
		if( $ignoreKeys.indexOf( key ) === -1 ){
			result[result.length] = $data[key];
		}
	}
	return result;
}
private function arrayCast( $data:Array, $isClone:Boolean, $type:String ):Array{
	var result:Array, i:int, j:int;
	if( $isClone ){
		result = $data.clone();
	}else{
		result = $data;
	}
	for( i = 0, j = result.length ; i < j ; ++i ){
		switch( $type ){
		case'int': result[i] = int( result[i] ); break;
		case'bool': result[i] = Boolean( result[i] ); break;
		case'num': result[i] = Number( result[i] ); break;
		case'str': result[i] = String( result[i] ); break;
		case'uint': result[i] = uint( result[i] ); break;
		}
	}
	return result;
}
final public function arrayCastInt( $data:Array, $isClone:Boolean = true ):Array{
	return arrayCast( $data, $isClone, 'int' );
}
final public function arrayCastUint( $data:Array, $isClone:Boolean = true ):Array{
	return arrayCast( $data, $isClone, 'uint' );
}
final public function arrayCastNumber( $data:Array, $isClone:Boolean = true ):Array{
	return arrayCast( $data, $isClone, 'num' );
}
final public function arrayCastString( $data:Array, $isClone:Boolean = true ):Array{
	return arrayCast( $data, $isClone, 'str' );
}
final public function arrayCastBoolean( $data:Array, $isClone:Boolean = true ):Array{
	return arrayCast( $data, $isClone, 'bool' );
}
final public function arrayTableColume( $data:Array, $col:int ):Array{
	var i:int, j:int, result:Array;
	result = [];
	for( i = 0, j = $data.length ; i < j ; ++i ){
		result[i] = $data[i][$col];
	}
	return result;
}
final public function arrayTableRow( $data:Array, $row:int ):Array{
	return $data[$row].concat();
}
final public function arrayHead( $data:Array, $length:int ):Array{
	return $data.slice( 0, $length );
}
final public function arraySlice( $data:Array, $start:int, $length:int ):Array{
	var i:int;
	i = $start+ $length;
	if( i > $data.length  ){
		i = $data.length;
	}
	var temp:*=$data.slice( $start, i );
	return $data.slice( $start, i );
}
final public function arraySplice( $data:Array, $start:int, $length:int ):Array{
	var i:int;
	i = $start+ $length - 1;
	if( i > $data.length - 1 ){
		i = $data.length - 1;
	}
	return $data.splice( $start, i );
}
final public function arrayEvenIndex( $data:Array ):Array{
	var result:Array, i:int, j:int;
	result = [];
	for( i = 0, j = $data.length ; i < j ; i += 2 ){
		result[result.length] = $data[i];
	}
	return result;
}
final public function arrayOddIndex( $data:Array ):Array{
	var result:Array, i:int, j:int;
	result = [];
	for( i = 1, j = $data.length ; i < j ; i += 2 ){
		result[result.length] = $data[i];
	}
	return result;
}
final public function arrayASC( $data:Array ):Array{
	var result:Array;
	result = $data.concat();
	result.sort( );
	return result;
}
final public function arrayDESC( $data:Array ):Array{
	var result:Array;
	result = $data.concat();
	result.sort( 2 );
	return result;
}
final public function arrayNumASC( $data:Array ):Array{
	var result:Array;
	result = $data.concat();
	result.sort( 16 );
	return result;
}
final public function arrayNumDESC( $data:Array ):Array{
	return arrayNumASC( $data ).reverse();
}
final public function arrayShuffle( $data:Array ):Array{
	var result:Array;
	result = $data.concat();
	result.sort( function():Number{return mathRand( 1, 10 ) < 5 ? 1 : -1; } );
	return result;
}
final public function arrayAddHead( $data:Array, $value:*, $isClone:Boolean = true ):Array{
	var result:Array, i:int, j:int;
	if( $isClone ){
		result = [];
	}else{
		result = $data;
	}
	for( i = 0, j = $data.length ; i < j ; ++i ){
		result[i] = $value + $data[i];
	}
	return result;
}
final public function arrayAddTail( $data:Array, $value:*, $isClone:Boolean = true ):Array{
	var result:Array, i:int, j:int;
	if( $isClone ){
		result = [];
	}else{
		result = $data;
	}
	for( i = 0, j = $data.length ; i < j ; ++i ){
		result[i] = $data[i] + $value;
	}
	return result;
}
final public function arrayMake( $length:*, ...$defaultValue ):Array{
	var result:Array, i:int, j:int, k:int;
	if( $length is Array ){
		$length = $length.length;
	}
	result = [];
	j = $defaultValue.length;
	if( j === 0 ){
		for( i = 0 ; i < $length ; ++i ){
			result[i] = i;
		}
	}else{
		for( i = 0, k = 0 ; i < $length ; ++i ){
			result[i] = $defaultValue[k++];
			if( k === j ){
				k = 0;
			}
		}
	}
	return result;
}
final public function arraySearch( $data:Array, $field:int, $search:* ):Array{
	var i:int, j:int;
	for( i = 0 , j = $data.length ; i < j ; ++i ){
		if( $data[i][$field] === $search ){
			return $data[i]; 
		}
	}
	return null;
}